package 数据结构.tree;

public class BinaryTreeDemo {
    public static void main(String[] args) {
        //创建一颗二叉树
        BinaryTree binaryTree = new BinaryTree();

        HeroNode node1 = new HeroNode(1, "宋江");
        HeroNode node2 = new HeroNode(2, "吴用");
        HeroNode node3 = new HeroNode(3, "卢俊义");
        HeroNode node4 = new HeroNode(4, "林冲");
        HeroNode node5 = new HeroNode(5, "关胜");
        binaryTree.setRoot(node1);
        node1.setLeft(node2);
        node1.setRight(node3);
        node3.setRight(node4);
        node3.setLeft(node5);

/*        System.out.println("前序遍历");
        binaryTree.preOrder();
        System.out.println("中序遍历");
        binaryTree.infixOrder();
        System.out.println("后序遍历");
        binaryTree.afterOrder();*/

     /*   System.out.println("前序遍历方式检索");
        //前序遍历的次数
        HeroNode heroNode = binaryTree.preOrdersSearch(5);
        if (heroNode != null) {
            System.out.printf("找到了,信息为:no=%d name=%s", heroNode.getNo(), heroNode.getName());
        }*/

      /*  System.out.println("中遍历方式检索");
        //前序遍历的次数
        HeroNode heroNode = binaryTree.infixOrdersSearch(5);
        if (heroNode != null) {
            System.out.printf("找到了,信息为:no=%d name=%s", heroNode.getNo(), heroNode.getName());
        }*/
       /* System.out.println("后序遍历方式检索");
        //前序遍历的次数
        HeroNode heroNode = binaryTree.afterOrdersSearch(5);
        if (heroNode != null) {
            System.out.printf("找到了,信息为:no=%d name=%s", heroNode.getNo(), heroNode.getName());
        }*/

        //测试删除
        System.out.println("删除前-------");
        //前序遍历
        binaryTree.preOrder();
        binaryTree.delete(5);
        System.out.println("删除后---------");
        //前序遍历
        binaryTree.preOrder();
    }
}

class BinaryTree {
    private HeroNode root;

    public void setRoot(HeroNode root) {
        this.root = root;
    }

    //前序遍历
    public void preOrder() {
        if (this.root != null) {
            this.root.preOredr();
        } else {
            System.out.println("二叉树为空,无法遍历");
        }
    }

    public void infixOrder() {
        if (this.root != null) {
            this.root.infixOrder();
        } else {
            System.out.println("二叉树为空,无法遍历");
        }
    }

    public void afterOrder() {
        if (this.root != null) {
            this.root.afterOrder();
        } else {
            System.out.println("二叉树为空,无法遍历");
        }
    }

    //查找方法:前序列
    public HeroNode preOrdersSearch(int no) {
        if (root != null) {
            return root.preOrdersearch(no);
        } else {
            return null;
        }
    }

    public HeroNode infixOrdersSearch(int no) {
        if (root != null) {
            return root.infixOrdersSearch(no);
        } else {
            return null;
        }
    }

    public HeroNode afterOrdersSearch(int no) {
        if (root != null) {
            return root.afterOrdersSearch(no);
        } else {
            return null;
        }
    }

    public void delete(int no) {
        if (root != null) {
            //如果只有一个root节点,这里立即判断root是不是要删除的元素
            if (root.getNo() == no) {
                root = null;
            } else {
                root.deletNode(no);
            }
        }
    }
}

class HeroNode {
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    //编写遍历的方法
    public void preOredr() {
        System.out.println(this);
        //递归向左子树前序遍历
        if (this.left != null) {
            this.left.preOredr();
        }
        if (this.right != null) {
            this.right.preOredr();
        }
    }

    //中序遍历
    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }

    //后序遍历
    public void afterOrder() {
        if (this.left != null) {
            this.left.afterOrder();
        }
        if (this.right != null) {
            this.right.afterOrder();
        }
        System.out.println(this);
    }

    //前序遍历查找
    public HeroNode preOrdersearch(int no) {
        System.out.println("一次前序产找");
        //先比较当前节点
        if (this.no == no) {
            return this;
        }
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.preOrdersearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.right != null) {
            resNode = this.right.preOrdersearch(no);
        }
        return resNode;
    }

    //中序遍历查找
    public HeroNode infixOrdersSearch(int no) {

        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.infixOrdersSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        System.out.println("中序遍历");
        if (this.no == no) {
            return this;
        }
        if (this.right != null) {
            resNode = this.right.infixOrdersSearch(no);
        }
        return resNode;
    }

    //后序遍历查找
    public HeroNode afterOrdersSearch(int no) {
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.afterOrdersSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.right != null) {
            resNode = this.right.afterOrdersSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        System.out.println("后序查找");
        if (this.no == no) {
            return this;
        }
        return resNode;
    }

    //删除节点
    public void deletNode(int no) {
        if (this.left != null && this.left.no == no) {
            this.left = null;
            return;
        }
        if (this.right != null && this.right.no == no) {
            this.right = null;
            return;
        }
        if (this.left != null) {
            this.left.deletNode(no);

        }
        if (this.right != null) {
            this.right.deletNode(no);
        }
    }
}
